/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.anim.sequence;

import gjt.Util;
import edu.idp.client.model.Cube3D;

import java.awt.*;

/**
 *
 * @author Kristopher T Babic
 */
public class CubeSequence extends Sequence {
    //private Vector    cells               = new Vector();
    private float xrot = 0.0f;
    private float yrot = 0.0f;
    private float zrot = 0.0f;
    private float inc = 10.0f;
    private Dimension size;
    private Cube3D cube;
    private float xfac;
    //private Component myComp;

    //	1-25 public CubeSequence () {}

    public CubeSequence() {//Component c) {
        super();//c);

        initImages();
        cube = new Cube3D();

    }

    //public void setComponent(Component c){ myComp = c;)
    private void initTransVariables() {
        size = new Dimension(50, 50);

        float xw = cube.getXMax() - cube.getXMin();
        float yw = cube.getYMax() - cube.getYMin();
        float zw = cube.getZMax() - cube.getZMin();
        if (yw > xw)
            xw = yw;
        if (zw > xw)
            xw = zw;
        float f1 = size.width / xw;
        float f2 = size.height / xw;

        xfac = 0.6f * (f1 < f2 ? f1 : f2) * 1.0f;
    }

    public Image getCurrentImage() {

        if (currentImage == null) {
            //System.out.println("ok2!");
            initTransVariables();
            currentImage = myComp.createImage(size.width, size.height);
            Util.waitForImage(myComp, currentImage);
        }
        //else
        //System.out.println("ugh2!");
        //if (currentImage == null)
        //{System.out.println("now what?");}
        //System.out.flush();

        return currentImage;

    }

    public Image getFirstImage() {
        return getNextImage();
    }

/* 	NEED TO FIND FASTER WAY TO DO THIS.  IT WILL CAUSE LAG AS MORE IMAGES ARE NEEDED */

    public Image getNextImage() {
//System.out.println("next");
        //System.out.flush();


        if (currentImage == null)
            currentImage = myComp.createImage(size.width, size.height);
//System.out.println("("+cube.getXMin()+","+cube.getYMin()+","+cube.getZMin()+"),("+cube.getXMax()+","+cube.getYMax()+","+cube.getZMax()+")");

        cube.setIdentity();
        cube.setTranslate((float) (-(cube.getXMin() + cube.getXMax()) / 2.0f),
                          (float) (-(cube.getYMin() + cube.getYMax()) / 2.0f),
                          (float) (-(cube.getZMin() + cube.getZMax()) / 2.0f));
        cube.setRotX(xrot);
        cube.setRotY(yrot);
        cube.setRotZ(zrot);
        cube.setScale(xfac, -xfac, (float) (16.0 * xfac / size.width));
        //cube.setScale(xfac, xfac, xfac);
        cube.setTranslate((float) (size.width / 2), (float) (size.height / 2), 0);
        cube.setTransform();
        Graphics g = currentImage.getGraphics();
        g.setColor(myComp.getBackground());
        g.fillRect(0, 0, size.width, size.height);
        cube.paint(g);
        g.dispose();

        increment();
         /**/

        //currentImage.getGraphics().drawRect(0,0,30,30);
        lastImagePainted = currentImage;
        return currentImage;

/*	    md.mat.unit();
        md.mat.translate(-(md.xmin + md.xmax) / 2,
                 -(md.ymin + md.ymax) / 2,
                 -(md.zmin + md.zmax) / 2);
        md.mat.mult(amat);
        md.mat.scale(xfac, -xfac, 16 * xfac / getSize().width);
        md.mat.translate(getSize().width / 2, getSize().height / 2, 8);
        md.transformed = false;
        md.paint(g);
        setPainted();
*/

    }

    public boolean isAtLastImage() {
        return false;
    }

    private void increment() {
        xrot = (xrot + inc) % 360.0f;
        zrot = (zrot + inc) % 360.0f;
        yrot = (yrot + inc) % 360.0f;
    }

    protected void initImages() {
        currentImage = null;
        lastImagePainted = null;
    }

    public String toString() {
        return "Rotating 3D Wire-frame Cube";
    }
}